<?php

/*

COMP 353F
Dr. B Desai
Final Project
Group #11

6330746 Nicholas CONSTANTINIDIS
9532862 Jacqueline FU
5484537 Claudio Javier LOPEZ FLORES
9218416 Que Tung NGUYEN

*/

  session_start();
?>

<!DOCTYPE html>
<html lang="en">
<head>
        <title>CoBAGSys - Home</title>
        <link type="text/css" rel="stylesheet" href="css/mystyle.css">
</head>
<body>
    <?php
      require 'menus.php';
    ?>
    <?php
		// Perform the date set
		function setDate() {
			$db = new Connect();
			
			$year = $_POST['year'];
			$month = $_POST['month'];
			$day = $_POST['day'];
			$hour = $_POST['hour'];
			$minute = $_POST['minute'];

			$yearend = $_POST['yearend'];
			$monthend = $_POST['monthend'];
			$dayend = $_POST['dayend'];
			$hourend = $_POST['hourend'];
			$minuteend = $_POST['minuteend'];
						  
			$sql = "UPDATE AdminSettings " .
				   "SET garageSaleStartDatetime = '" . $year . "-" . str_pad($month, 2, "0", STR_PAD_LEFT) . "-" . str_pad($day, 2, "0", STR_PAD_LEFT) . " " . str_pad($hour, 2, "0", STR_PAD_LEFT) . ":" . str_pad($minute, 2, "0", STR_PAD_LEFT) . "', " .
				   "garageSaleEndDatetime = '" . $yearend . "-" . str_pad($monthend, 2, "0", STR_PAD_LEFT) . "-" . str_pad($dayend, 2, "0", STR_PAD_LEFT) . " " . str_pad($hourend, 2, "0", STR_PAD_LEFT) . ":" . str_pad($minuteend, 2, "0", STR_PAD_LEFT) . "'";
			
			$result = $db->query($sql);
			
			if(!$result || mysql_affected_rows() == 0) // Failed date set
			{
				return false;
			}
			else // New garage sale set
			{
				return true;
			}
		}
		
		// Get the garage sale dates
		function getDates() {
			$db = new Connect();
			
			$sql = "SELECT year(garageSaleStartDatetime) AS ys, " .
				   "month(garageSaleStartDatetime) AS ms, " .
				   "day(garageSaleStartDatetime) AS ds, " .
				   "hour(garageSaleStartDatetime) AS hhs, " .
				   "minute(garageSaleStartDatetime) AS mms, " .
				   "year(garageSaleEndDatetime) AS ye, " .
				   "month(garageSaleEndDatetime) AS me, " .
				   "day(garageSaleEndDatetime) AS de, " .
				   "hour(garageSaleEndDatetime) AS hhe, " .
				   "minute(garageSaleEndDatetime) AS mme " .				   
				   "FROM AdminSettings";
				   			   
			$result = $db->query($sql);
			
			if(!$result || mysql_num_rows($result) == 0) // Couldn't find dates
			{
				return;
			}
			else // Return date info
			{
				$rows = $db->to_array($result);
				return $rows[0];
			}
		}
		
		// Check to see if we received post data and perform date set
		// TODO: Check for empty fields and valid date value
		$setdate_failed = false;
		if (isset($_POST["setdate"]))
			$setdate_failed = !setDate();
    ?>
    <div id="main">
      	<p class="title">
      		Administration
      	</p>
<?php
	if (isAdmin()) {
		print "<input type=\"button\" class=\"largeButton\" value=\"View reports\" onclick=\"parent.location='adminreports.php'\"/> ";
		print "<input type=\"button\" class=\"largeButton\" value=\"Set parameters\" onclick=\"parent.location='adminparam.php'\"/> ";
		print "<input type=\"button\" class=\"largeButton\" value=\"Search members\" onclick=\"parent.location='adminmembers.php'\"/> ";
		print "<input type=\"button\" class=\"largeButton\" value=\"Edit categories\" onclick=\"parent.location='admincategories.php'\"/> ";
		print "<p class=\"title\"> ";
			print "Garage sale";
		print "</p> ";
		print "<form id=\"garageForm\" action=\"\" method=\"post\"> ";
		
		if ($setdate_failed == true)
			print "<p class=\"error\">FAILED SETTING NEW DATE!</p> ";

		$dates = getDates();
			
		print "<table border=\"0\">";
		print "<tr>";
		print "<td><span class=\"normal\">Start date: </span></td>";
		print "<td><input type=\"text\" maxlength=\"4\" size=\"4\" name=\"year\" value=\"" . $dates['ys'] . "\"></td>";
		print "<td><input type=\"text\" maxlength=\"2\" size=\"2\" name=\"month\" value=\"" . $dates['ms'] . "\"></td>";
		print "<td><input type=\"text\" maxlength=\"2\" size=\"2\" name=\"day\" value=\"" . $dates['ds'] . "\"></td>";
		print "<td><input type=\"text\" maxlength=\"2\" size=\"2\" name=\"hour\" value=\"" . $dates['hhs'] . "\"></td>";
		print "<td><input type=\"text\" maxlength=\"2\" size=\"2\" name=\"minute\" value=\"" . $dates['mms'] . "\"></td>";
		print "</tr>";
		print "<tr>";
		print "<td><span class=\"normal\">End date: </span></td>";
		print "<td><input type=\"text\" maxlength=\"4\" size=\"4\" name=\"yearend\" value=\"" . $dates['ye'] . "\"></td>";
		print "<td><input type=\"text\" maxlength=\"2\" size=\"2\" name=\"monthend\" value=\"" . $dates['me'] . "\"></td>";
		print "<td><input type=\"text\" maxlength=\"2\" size=\"2\" name=\"dayend\" value=\"" . $dates['de'] . "\"></td>";
		print "<td><input type=\"text\" maxlength=\"2\" size=\"2\" name=\"hourend\" value=\"" . $dates['hhe'] . "\"></td>";
		print "<td><input type=\"text\" maxlength=\"2\" size=\"2\" name=\"minuteend\" value=\"" . $dates['mme'] . "\"></td>";
		print "<td><input type=\"submit\" class=\"smallButton\" name=\"setdate\" value=\"Set date\"/></td>";
		print "</tr>";
		print "</table>";
		print "</form> ";
	}
	else
	{
		print "<p class=\"error\">ACCESS DENIED!</p>";
	}
?>
    </div>
</body>
</html>